package VerticalPrototypes;

import java.sql.*;

/**
 * @author Tyler
 */

//Class for adding a friend's ID to user's friend list. 
//DOES NOT HANDLE DUPLICATE FRIENDS.
public class AddFriend {
    
    //Instance of this class being used.
    private static AddFriend instance = null;
    
    public AddFriend()
    {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        ResultSet rs2 = null;

        //Hardcoded database location.
        String url = "snyamathi.dyndns.org:37965";
        String user = "root";
        String password = "baconatmidnight";
        
        //Hardcoded prototype user_id, item_id.
        int user_id = 1;
        int friend_id = 2;
        
        //Don't allow a user to be friends with themselves. 
        if(user_id != friend_id)
            try {
                //Connect to the databse.
                con = DriverManager.getConnection(url, user, password);
                st = con.createStatement();

                //Get user's existing friends list.
					//Tyler's manual test string: SELECT friendIDList FROM accounts WHERE userID = 1;
                if(rs == (st.executeQuery("SELECT friendIDList FROM accounts WHERE userID = " + user_id)))
                { /*Success; Got a user's existing friends list. */ }
                else{ /*Not success.*/ }
                
                //Now actually update the friends list - append new friend to the end of the list.
					//Tyler's manual test string: UPDATE accounts SET friendIDList = '2, 3' WHERE userID = 1;
                if(rs2 == (st.executeQuery("UPDATE accounts SET friendIDList = '" + rs + ", " + friend_id +"' WHERE userID = " + user_id)))
                { /*Success; Added friend ID to the list. */ }
                else{ /*Not success.*/ }

                //Prints out the updated friends list.
                System.out.println(rs2);

            } catch (SQLException ex) {
                    //Something happens.
            } finally {
                //Close connections safely - May not need to do this? Overkill?
                try {
                    if (rs != null) {
                        rs.close();
                    }
                    if (st != null) {
                        st.close();
                    }
                    if (con != null) {
                        con.close();
                    }

                } catch (SQLException ex) {
                        //Something happens.
                }
            }
        }
    
    //Execute the setting of an Item's status in a user's inventory.
    public static AddFriend user_AddFriend()
    {
        if(instance == null)
            instance = new AddFriend();
        return instance;
    }  
}